package com.example.demo7;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * 自定义的内容提供者
 */
public class MyPersonProvider extends ContentProvider {
    //定义一个Uri的路径匹配器，如果路径匹配不成功返回-1
    private static UriMatcher mUriMatcher=new UriMatcher(-1);
    //定义一个状态判路径是否成功
    private static final int SUCESS=1;
    //实例化数据库帮助类
    private MyDbHelper myDbHelper;
    //定义数据库表的名称
    private static final String sqlname="info";

    /*
    定义一个静态代码块，验证路径的匹配
     */
    static
    {
        //参数1.包名、参数2.表名、参数3.匹配成功返回值
        mUriMatcher.addURI("com.example.demo7",sqlname,SUCESS);
    }

    public MyPersonProvider() {
    }

    //删除
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
//        throw new UnsupportedOperationException("Not yet implemented");

        //获取匹配码
        int code=mUriMatcher.match(uri);
        //判断匹配码是否与成功码一致
        if (code==SUCESS)
        {
            //通过数据库帮助类，创建数据库操作对象
            SQLiteDatabase database=myDbHelper.getWritableDatabase();
            //执行插入命令
            int count = database.delete(sqlname,selection,selectionArgs);
            //判断是否插入内容(数据库内容是否发生变化)
            if(count>0)
            {
                //提示数据库内容发生变化了
                getContext().getContentResolver().notifyChange(uri,null);
            }
            database.close();
            return count;
        }else
        {
            throw new IllegalArgumentException("路径不正确，无法插入数据！");
        }
    }
    //获取类型
    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
//        throw new UnsupportedOperationException("Not yet implemented");
        return null;
    }
    //添加
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
//        throw new UnsupportedOperationException("Not yet implemented");
        //获取匹配码
            int code=mUriMatcher.match(uri);
                //判断匹配码是否与成功码一致
                if (code==SUCESS)
                {
                    //通过数据库帮助类，创建数据库操作对象
                    SQLiteDatabase database=myDbHelper.getWritableDatabase();
                    //执行插入命令
                    long insert = database.insert(sqlname,null,values);
                    //判断是否插入内容(数据库内容是否发生变化)
                    if(insert>0)
                    {
                        //重新构建一个Uri,withAppendedId的第一个参数：方法中的uri，第二个参数：插入后的返回值
                        Uri mUri= ContentUris.withAppendedId(uri,insert);
                        //提示数据库内容发生变化了
                        getContext().getContentResolver().notifyChange(mUri,null);
                        return mUri;
                    }
                    database.close();
                    return uri;
                }else
                 {
                   throw new IllegalArgumentException("路径不正确，无法插入数据！");
                 }
    }
    //创建时的方法
    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.

        myDbHelper=new MyDbHelper(getContext());//谁调用谁实例化
        return false;
    }
    //查询
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
//        throw new UnsupportedOperationException("Not yet implemented");
        //获取匹配码
        int code=mUriMatcher.match(uri);
        //判断匹配码是否与成功码一致
        if (code==SUCESS)
        {
            //通过数据库帮助类，创建数据库操作对象
            SQLiteDatabase database=myDbHelper.getReadableDatabase();
            return database.query(sqlname, projection, selection, selectionArgs, null,null,sortOrder);
        }else
        {
            throw new IllegalArgumentException("路径不正确，无法查询数据！");
        }

    }
    //修改
    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
//        throw new UnsupportedOperationException("Not yet implemented");
        //获取匹配码
        int code=mUriMatcher.match(uri);
        //判断匹配码是否与成功码一致
        if (code==SUCESS)
        {
            //通过数据库帮助类，创建数据库操作对象
            SQLiteDatabase database=myDbHelper.getWritableDatabase();
            //执行更新命令    参数1.表名 参数2.方法中的values 参数3.方法中的selection、参数4.方法中的selectionArgs
            int count = database.update(sqlname,values,selection,selectionArgs);
            //判断是否插入内容(数据库内容是否发生变化)
            if(count>0)
            {
                getContext().getContentResolver().notifyChange(uri,null);
            }
            database.close();
            return count;
        }else
        {
            throw new IllegalArgumentException("路径不正确，无法修改数据！");
        }
    }
}
